﻿// 文件名：复习题
// 创建时间：2021/6/22/星期二 11:01:03

#include <iostream>
using namespace std;

//编写一个接收三个参数的函数：int数组名、数组长度、int值，
//并将所有的元素都设置为该int值
const int SIZE = 10;
void setValue(int* arr,int len ,int n)
{
	for (int i = 0; i < len; i++)
	{
		arr[i] = n;
	}
}
void review3()
{
	int arr[SIZE];
	int value;
	cout << "请输入填充数组的整数： ";
	cin >> value;
	setValue(arr, SIZE, value);
	for (int i = 0; i < SIZE; i++)
	{
		cout << "arr[" << i << "] = " << arr[i] << '\t';
	}
	cout << "Done\n";

}

//复习题4
void setValue(int* start, int* end, int n);
void review4()
{
	int arr[SIZE];
	int value = 5;
	setValue(arr, arr + SIZE, value);
	for (int i = 0; i < SIZE; i++)
	{
		cout << "arr[" << i << "] = " << arr[i] << '\n';
	}
	cout << "\nDone\n";
}
void setValue(int* start, int* end, int n)
{
	int j = 1;
	for (int i = 0; i < SIZE; i++)
	{
		if (i + j < SIZE)
		{
			*(start + i) = n;
			*(end - j) = n;
			cout << "i = " << i << ",j = " << j << endl;
		}
		j++;
	}
}

//复习题5
double findMax(const double* arr, int len);
void review5()
{
	double arr[5] = { 1,5,7,2,4 };
	int len = sizeof(arr) / sizeof(arr[0]);
	double max = findMax(arr, len);
	cout << "数组最大值为： " << max << endl;
}
double findMax(const double* arr, int len)
{
	double max = arr[0];
	for (int i = 0; i < len; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}
	return max;
}

//复习题8
int replace(char* str, char c1, char c2);
void review8()
{
	char str[10] = "minimum";
	char c1 = 'm';
	char c2 = '@';
	cout << "替换前的字符串： " << str << endl;
	int times = replace(str, c1, c2);
	cout << "替换后的字符串： " << str << endl;
	cout << "替换次数： " << times << endl;
}
int replace(char* str, char c1, char c2)
{
	int len = strlen(str);
	int count = 0;
	cout << "字符串长度为： " << len << endl;
	for (int i = 0; i < len; i++)
	{
		if (str[i] == c1)
		{
			str[i] = c2;
			count++;
		}
	}
	return count;
}

//复习题12
#include <string.h>
struct applications {
	char name[30];
	int credit_ratings[3];
};
void showApp(applications app);
void showApp(const applications* app);
void review12()
{
	applications app = { "The best language is CHINESE",{1,2,3} };
	/*app.name = "The best language is CHINESE"; // C/C++只允许字符数组初始化时的赋值操作
	app.credit_ratings = { 10,20,30 };*/
	strcpy_s(app.name, "hahaha");
	cout << app.name << endl;
	cout << app.credit_ratings[0] << endl;
	showApp(app);
	showApp(&app);
}
void showApp(applications app)
{
	cout << "结构体值传递\n";
	cout << "app.name = " << app.name << endl;
	cout << "app.credit_ratings = " 
		<< app.credit_ratings[0]
		<<" "<< app.credit_ratings[1]
		<<" "<< app.credit_ratings[2]
		<< endl;

}
void showApp(const applications* app)
{
	cout << "结构体指针传递\n";
	cout << "app.name = " << app->name << endl;
	cout << "app.credit_ratings = "
		<< app->credit_ratings[0]
		<< " " << app->credit_ratings[1]
		<< " " << app->credit_ratings[2]
		<< endl;
}

int main()
{
	//复习题3
	//review3();

	//复习题4
	//review4();

	//复习题5
	//review5();

	//复习题8
	//review8();

	//复习题12
	review12();

	system("pause");
	return 0;
}